package yxy.game.pm2.command.niubee;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.weapongrade;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.NiuBeeMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.niubee.NiuBeeModule;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

/** 神器 铸魂升级 */
@MessageType(proto = Command.NiuBeeSoulUpgradesou, classtype = NiuBeeMessage.NiuBeeSoulUpgradesouRequest.class)
public class CMD_NiuBeeSoulUpgradesou extends Command<NiuBeeMessage.NiuBeeSoulUpgradesouRequest> {

	@Override
	public void execute(MessageSource messageSource, NiuBeeMessage.NiuBeeSoulUpgradesouRequest request) {
		String playerCode = messageSource.getTag();
		int times = request.getTimes();

		NiuBeeMessage.NiuBeeSoulUpgradesouResponse.Builder response = NiuBeeMessage.NiuBeeSoulUpgradesouResponse.newBuilder();

		NiuBeeModule niuBeeModule = NiuBeeModule.get(playerCode);
		try {
			if (niuBeeModule.tryLock()) {
				int soul = niuBeeModule.getSoulLevel();
				weapongrade data = DataCenter.getData(niuBeeModule.getLevel(), weapongrade.class);
				int max = data.getCastmax();
				if (times <= 0 || soul + times > max) {
					send(messageSource, response, Result.NiubeeSoulUpper);// 神器铸魂已经升满
					return;
				}
				// 道具固定1029
				Currency cost = Currency.create(CurrencyType.CT_物品, 1029, times);
				try {
					new CurrencyHandle().cost(playerCode, CCReason.神器, cost);
				} catch (CurrencyNotEnoughException e) {
					send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
					return;
				}
				niuBeeModule.setSoulLevel(soul + times);
				niuBeeModule.set();

				response.setSoul(niuBeeModule.getSoulLevel());
				send(messageSource, response, Result.Success);

				PlayTypeHandle.saveWeapon(playerCode, PlayTypeModule.get(playerCode));
				new BattleHandle().powerUpdate(playerCode);// 战力更新

				if (niuBeeModule.getSoulLevel() + 1 >= max) {
					new TipHandle().updateTip(playerCode, TipType.神器_铸魂按钮, false);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			niuBeeModule.unlock();
		}
	}

	private void send(MessageSource messageSource, NiuBeeMessage.NiuBeeSoulUpgradesouResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}